package com.ych.modules.api.unAuthorInfc.login.controller;

import com.ych.modules.api.unAuthorInfc.login.entity.LoginVo;
import com.ych.modules.api.unAuthorInfc.login.entity.ModifyVo;
import com.ych.modules.api.unAuthorInfc.login.entity.RegisterVo;
import com.ych.modules.api.unAuthorInfc.login.service.IApiUserInfoService;
import com.ych.modules.base.dbEntity.TUserInfoEntity;
import com.ych.redis.RedisLockUtil;
import com.ych.redis.config.RedisKeyConfig;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/5
 * Time: 15:07
 * Description:
 */
@RestController
@RequestMapping("/api/unAuth")
@Api(tags = {"API——登录相关"}, description = "API——登录相关", position = 2)
public class LoginController {
    @Autowired
    private IApiUserInfoService iApiUserInfoService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public R<Map<String, Object>> login(@RequestBody LoginVo loginVo) {
        try {
            Map<String, Object> map = iApiUserInfoService.login(loginVo);
            return new R(0, "请求成功", map);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public R<Map<String, Object>> register(@RequestBody RegisterVo registerVo) {
        String key = RedisKeyConfig.registerCode + registerVo.getMobile();
        try {
            //上锁 不解锁
            if (RedisLockUtil.tryLock(key, key, RedisKeyConfig.leaseTime)) {
                TUserInfoEntity userInfoEntity = iApiUserInfoService.register(registerVo);
                //直接登录
                LoginVo loginVo = new LoginVo();
                loginVo.setMobile(userInfoEntity.getMobile());
                loginVo.setPassword(userInfoEntity.getPassword());
                Map<String, Object> map = iApiUserInfoService.login(loginVo);
                return new R(0, "成功", map);
            }
            return new R(0, "正在进行处理，请勿重复操作！", null);
        } catch (Exception e) {
            //出了异常，进行解锁
            RedisLockUtil.unlock(key, key);
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "注册短信获取验证码", position = 4)
    @PostMapping("/getSmsCaptcha")
    public R<String> getSmsCaptcha(@RequestBody String mobile) {
        try {
            iApiUserInfoService.getSmsCaptcha(mobile);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "修改密码", position = 4)
    @PostMapping("/modifyPassword")
    public R<Map<String, Object>> modifyPassword(@RequestBody ModifyVo modifyVo) {
        try {
            if (StringUtils.isBlank(modifyVo.getCode())
                    && StringUtils.isBlank(modifyVo.getMobile())
                    && StringUtils.isBlank(modifyVo.getNewPwd())) {
                throw new RuntimeException("请填写所需所有信息！");
            }
            iApiUserInfoService.modifyPassword(modifyVo);
            //直接登录
            LoginVo loginVo = new LoginVo();
            loginVo.setMobile(modifyVo.getMobile());
            loginVo.setPassword(modifyVo.getNewPwd());
            Map<String, Object> map = iApiUserInfoService.login(loginVo);
            return new R(0, "成功", map);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
